//
//  KZPrepareExamVC.m
//  lg_zhinengbeikao_english
//
//  Created by 朱凯 on 2018/12/26.
//  Copyright © 2018 zhukai. All rights reserved.
//

#import "KZReviewExamVC.h"

// vc
#import "KZReviewExamMoreVC.h"              // 更多
#import "KZMakeExamVC.h"                    // 做试卷
#import "KZMakeExamVC_v2.h"

// view
#import "KZReviewInformationView.h"         // 信息视图
#import "KZReviewSearchAndFilterView.h"     // 搜索和筛选
#import "KZFilterView.h"                    // 筛选视图

// cell
#import "KZReviewTestPaperCell.h"           // 试卷
#import "KZReviewClassTitleCell.h"          // 分类


@interface KZReviewExamVC () <UITableViewDelegate, UITableViewDataSource>

@property (nonatomic, strong) UITableView *table;

@property (nonatomic, strong) KZReviewInformationView *reviewInformationView;
@property (nonatomic, strong) KZReviewSearchAndFilterView *searchFilterView;


@end

@implementation KZReviewExamVC
{
    // 记录上次选中的筛选标签
    NSArray *_filterSelectedTags;
}

#pragma mark - vc生命周期
- (void)viewDidLoad {
    [super viewDidLoad];

    [self setupUI];
}
- (void)viewWillAppear:(BOOL)animated {
    
    [super viewWillAppear:animated];
    [self setNaviAttributeWithTranslucent:true stateBarLightContent:true];
}

#pragma mark - 视图
- (void)setupUI {
    
    // 列表
    _table = [[UITableView alloc] init];
    _table.estimatedRowHeight = 200;
    _table.rowHeight = UITableViewAutomaticDimension;
    _table.backgroundColor = kBgColor;
    _table.tag = 666;
    _table.separatorStyle = UITableViewCellSeparatorStyleNone;
    _table.delegate = self;
    _table.dataSource = self;
    [self.view addSubview:_table];
    [_table mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.equalTo(self.view).insets(UIEdgeInsetsMake(0, 0, 0, 0)).priorityMedium();
        if (@available(iOS 11.0, *)) {
            make.bottom.equalTo(self.view.mas_safeAreaLayoutGuideBottom).offset(-0);
            make.left.equalTo(self.view.mas_safeAreaLayoutGuideLeft);
            make.right.equalTo(self.view.mas_safeAreaLayoutGuideRight);
        }
    }];
    
    [_table registerClass:[KZReviewTestPaperCell class] forCellReuseIdentifier:@"paperCell"];
    [_table registerClass:[KZReviewClassTitleCell class] forCellReuseIdentifier:@"classCell"];

    
    UIView *header = [[UIView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, H(220) + H(70))];
    _table.tableHeaderView = header;
    
    // 信息
    _reviewInformationView = [KZReviewInformationView new];
    [header addSubview:_reviewInformationView];
    [_reviewInformationView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.equalTo(header);
        make.top.mas_equalTo(0);
        make.height.mas_equalTo(header.height);
    }];
}

#pragma mark - UITableViewDatasource
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    
    return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return 10;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    if (indexPath.row % 4 == 0) {
        KZReviewClassTitleCell *cell = [tableView dequeueReusableCellWithIdentifier:@"classCell"];
        return cell;
    } else {
        KZReviewTestPaperCell *cell = [tableView dequeueReusableCellWithIdentifier:@"paperCell"];
        cell.testStatus = indexPath.row % 3;
        return cell;
    }
    
}
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
    if (section == 0) {
        return 60;
    } else {
        return 0;
    }
}
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
    
    // 搜索和筛选
    _searchFilterView = [KZReviewSearchAndFilterView new];
    @weakify(self);
    _searchFilterView.onTouchFilterCallback = ^{
        @strongify(self);
        // 开始筛选
        [self beginFilter];
    };
    return _searchFilterView;
}
- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section {
    return 0;
}
- (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section {
    return UIView.new;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    [tableView deselectRowAtIndexPath:indexPath animated:false];
    if (indexPath.row % 4 == 0) {
        KZReviewExamMoreVC *vc = [KZReviewExamMoreVC new];
        [self present:vc];
    } else {
        KZMakeExamVC_v2 *vc = [KZMakeExamVC_v2 new];
        [self present:vc];
    }
}

#pragma mark - 事件


#pragma mark - 辅助方法
// 开始筛选
- (void)beginFilter {
    
    KZFilterView *filterView = [KZFilterView new];
    filterView.lateseSelectedTags = self->_filterSelectedTags;
    filterView.tag = 777; // 记录tag，再次点击需要隐藏
    filterView.onConfirmCallback = ^(NSArray *selectedTags) {
        // 记录上次选中的筛选标签
        self->_filterSelectedTags = selectedTags;
        // 设置筛选按钮是否高亮（是否有筛选条件）
        self->_searchFilterView.isInFilter = selectedTags != nil;
    };

    // 转换搜索栏的位置到view上
    CGFloat y = [self.table convertPoint:self.searchFilterView.origin toView:self.view].y;
    // 在弹出筛选框时，table‘不能滑动，因此可以用这个参数来判断是否弹出
    if (self.table.scrollEnabled) {
        [UIView animateWithDuration:0.25 animations:^{
            self.table.contentOffset = CGPointMake(0, self.table.contentOffset.y + y);
        } completion:^(BOOL finished) {
            [filterView showAtY:self.searchFilterView.height basalView:self.view];
        }];
    } else {
        KZFilterView *filterView = [self.view viewWithTag:777];
        [filterView hide];
    }
}

@end
